//
//  EDSVideoPreviewView.m
//  DrivieSchoolStudents
//
//  Created by TangBin on 2019/5/23.
//  Copyright © 2019 班文政. All rights reserved.
//

#import "EDSVideoPreviewView.h"
#import <AVFoundation/AVFoundation.h>

@interface EDSVideoPreviewView ()
@property (nonatomic, strong) UIImageView *playImgView;
@property (nonatomic, strong) NSURL *videoUrl;
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) AVPlayerLayer *playerLayer;
@property (nonatomic, strong) AVPlayerItem *playerItem;

@property (nonatomic, assign) BOOL isPlaying;

- (void)initWithPlayer;

- (void)initWithPlayImgView;

@end

@implementation EDSVideoPreviewView

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initWithFrame:(CGRect)frame videoUrl:(NSURL *)videoUrl
{
    self.videoUrl = videoUrl;
    return [self initWithFrame:frame];
}

- (void)initWithVar
{
    [super initWithVar];
    
    
}

- (void)initWithSubView
{
    [super initWithSubView];
    // com_play_big_ic
    
    [self initWithPlayer];
    
    [self initWithPlayImgView];
    
    @weakify(self);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        @strongify(self);
        [self startPlay];
    }];
    [self addGestureRecognizer:tap];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];
}

- (void)initWithPlayer
{
    if (!_player)
    {
        _playerItem = [[AVPlayerItem alloc] initWithURL:self.videoUrl];
        self.player = [[AVPlayer alloc] initWithPlayerItem:_playerItem];
        _playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
        _playerLayer.backgroundColor = [UIColor blackColor].CGColor;
        _playerLayer.frame = self.bounds;
        _playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
        [self.layer addSublayer:_playerLayer];
        self.player.volume = 1.0f;
        
        // 监听status属性
//        [self.playerItem addObserver:self forKeyPath:<#(nonnull NSString *)#> options:<#(NSKeyValueObservingOptions)#> context:<#(nullable void *)#>]
        
    }
}

- (void)initWithPlayImgView
{
    if (!_playImgView)
    {
        CGRect frame = CGRectMake(0, 0, 69, 69);
        _playImgView = [[UIImageView alloc] initWithFrame:frame];
        _playImgView.image = [UIImage imageNamed:@"com_play_big_ic"];
        _playImgView.center = CGPointMake(self.width * 0.5, self.height * 0.5);
        [self addSubview:_playImgView];
    }
}

- (void)startPlay
{
    if (self.isPlaying)
    {
        [self.player pause];
        self.isPlaying = NO;
        self.playImgView.hidden = NO;
    }
    else
    {
        [self.player play];
        self.isPlaying = YES;
        self.playImgView.hidden = YES;
    }
}

- (void)stopPlay
{
    if (self.isPlaying)
    {
        self.playImgView.hidden = NO;
        [self.player pause];
    }
}

/**
 收到播放完成的通知

 @param note note description
 */
- (void)playbackFinished:(NSNotification *)note
{
//    Plog(@"视频播放完成");
    self.isPlaying = NO;
    self.playImgView.hidden = NO;
    [self.player seekToTime:CMTimeMake(0, 1)];
    [self.player play];
}


@end
